<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1"))
$bool=verifLibelle1('Code_agent') && verifLibelle('Nom_agent')&& verifLibelle('Prenom_agent') && verifLibelle('Identifiant_unite') ;

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && $bool && !existeChamp($database_microassur, $microassur,'agent','Code_agent',@$_POST['Code_agent'])) {
  $insertSQL = sprintf("INSERT INTO agent (Identifiant_agent, Code_agent, Nom_agent, Prenom_agent, Etat_agent, Identifiant_unite) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_agent'], "int"),
                       GetSQLValueString($_POST['Code_agent'], "text"),
                       GetSQLValueString($_POST['Nom_agent'], "text"),
                       GetSQLValueString($_POST['Prenom_agent'], "text"),
                       GetSQLValueString("A", "text"),
                       GetSQLValueString($_POST['Identifiant_unite'], "int"));

  mysql_select_db($database_microassur, $microassur);
  mysql_query("START TRANSACTION");
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
  if($Result1){ mysql_query("commit");header("location:index.php?ajout=success");}

else { mysql_query("rollback");$message=colorer("Contacter la DSI, erreur création agent","false") ;}
}
elseif(isset($_POST["MM_insert"])&& !$bool)
{
if(empty($_POST['Code_agent'])) $message=colorer("Vous devez saisir le code","false");
elseif(!verifLibelle1('Code_agent')) $message=colorer("Le code saisi comporte des caractères spéciaux interdits","false");

elseif(empty($_POST['Nom_agent'])) $message=colorer("Vous devez saisir le nom ","false");
elseif(!verifLibelle('Nom_agent')) $message=colorer("Le nom saisi comporte des caractères spéciaux interdits ","false");
//elseif(empty($_POST['Prenom_agent'])) /* $message=colorer("Vous devez saisir le prénom ","false"); */  { $a=verifLibelle('Prenom_agent'); var_dump($a); }
elseif(!verifLibelle('Prenom_agent')) $message=colorer("Vous devez saisir le prénom ","false");
}

if(isset($_POST["MM_insert"]) && existeChamp($database_microassur, $microassur,'agent','Code_agent',@$_POST['Code_agent']))$message=colorer("Cet agent existe déjà","false");

mysql_select_db($database_microassur, $microassur);
$query_rs = "SELECT * FROM agent WHERE Etat_agent = 'A'";
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);

mysql_select_db($database_microassur, $microassur);
$query_rs_unite = "SELECT * FROM unite";
$rs_unite = mysql_query($query_rs_unite, $microassur) or die(mysql_error());
$row_rs_unite = mysql_fetch_assoc($rs_unite);
$totalRows_rs_unite = mysql_num_rows($rs_unite);
?>
<?php  if(isset($_GET['ajout']) && $_GET['ajout']='success') $message=colorer("L'agent est créé","true") ;?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Agent";
					$nommodule2="Liste des agents";
					$nom_module_saisie="Ecran saisie";
					?>
			</div>
			<div class="cleared"></div>
		<div class="corps1">
			<!--/***************************************/-->
			
			<div class="imgleft"></div>
			<div class="imgmiddle">
			<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
			<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
			<div class="cleared"></div>
                <table class="tab_list bordh">
                  <tr id="niv1">
                    <td class="col1">CODE</td>							
                    <td class="col2">NOM</td>
                    <td class="col3">PRENOM</td>
                    <td class="col4">UNITE</td>
                  </tr>
                </table>
				<div class="content_table">
					<table class="tab_list">
						<?php $i=1; do { 
						  
						  $codeunite_rs_unit_liste = "-1";
						if (isset($row_rs['Identifiant_unite'])) {
						  $codeunite_rs_unit_liste = $row_rs['Identifiant_unite'];
						}
						mysql_select_db($database_microassur, $microassur);
						$query_rs_unit_liste = sprintf("SELECT * FROM unite WHERE Identifiant_unite = %s", GetSQLValueString($codeunite_rs_unit_liste, "text"));
						$rs_unit_liste = mysql_query($query_rs_unit_liste, $microassur) or die(mysql_error());
						$row_rs_unit_liste = mysql_fetch_assoc($rs_unit_liste);
						$totalRows_rs_unit_liste = mysql_num_rows($rs_unit_liste);
						if ($totalRows_rs > 0) { // Show if recordset not empty ?>
							<tr <?php if($i%2==0) echo 'class="colorligne"'; ?> >
							  <td class="col1"><a href="modagent.php?Identifiant_agent=<?php echo $row_rs['Identifiant_agent']; ?>"><?php echo $row_rs['Code_agent']; ?></a></td>
							  <td class="col2"><a href="modagent.php?Identifiant_agent=<?php echo $row_rs['Identifiant_agent']; ?>"><?php echo $row_rs['Nom_agent']; ?></a></td>
							  <td class="col3"><a href="modagent.php?Identifiant_agent=<?php echo $row_rs['Identifiant_agent']; ?>"><?php echo $row_rs['Prenom_agent']; ?></a></td>
							  <td class="col4"><a href="modagent.php?Identifiant_agent=<?php echo $row_rs['Identifiant_agent']; ?>"><?php echo $row_rs_unit_liste['Nom_unite']; ?></a></td>
							</tr>
							  <?php } // Show if recordset not empty ?>
									<?php $i++; } while ($row_rs = mysql_fetch_assoc($rs)); ?>

						</table>
						</div>
					</div>
					<div class="imgright"></div>
				<div class="cleared"></div>
				<!--/***************************************/-->
				<div id="br"></div>
				
						<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
						<div class="imgleft"></div>
						<div class="imgmiddle">
							<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
							<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nom_module_saisie; ?></h5></span><span class="btnright"></span></div>
								<div class="formulaire">
									<table class="tabform">
										<tr>
										  <td>CODE</td>
										  <td><input class="resize_1" type="text" name="Code_agent"  maxlength="10" value="<?php if(isset($_POST['Code_agent']))echo remind_text('Code_agent'); ?>" size="32" /></td>
									</tr>
									<tr>
									  <td valign="top">
									  NOM</td>
									  <td><input name="Nom_agent" type="text"  maxlength="30"value="<?php if(isset($_POST['Nom_agent']))echo remind_text('Nom_agent'); ?>" size="40" v="v" />      </td>
									</tr>
									<tr>
									  <td valign="top">PRENOM</td>
									  <td><input name="Prenom_agent"  maxlength="30"type="text" value="<?php if(isset($_POST['Prenom_agent']))echo remind_text('Prenom_agent'); ?>" size="40" />      </td>
									</tr>
									<tr>
									  <td>UNITE</td>
									  <td><select name="Identifiant_unite">
										<?php
								do {  
								?>
										<option value="<?php echo $row_rs_unite['Identifiant_unite']?>"><?php echo $row_rs_unite['Nom_unite']?></option>
										<?php
								} while ($row_rs_unite = mysql_fetch_assoc($rs_unite));
								  $rows = mysql_num_rows($rs_unite);
								  if($rows > 0) {
									  mysql_data_seek($rs_unite, 0);
									  $row_rs_unite = mysql_fetch_assoc($rs_unite);
								  }
								?>
											</select>      
										</td>
									</tr>
								</table>
							</div>
					</div>
					<div class="imgright"></div>				
						<div class="cleared"></div>
						
							<div class="bt1"><a href="<?php echo $retour; ?>"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
							<div class="bt"><span class="btleft"></span><input type="submit" name="Creer" value="Créer" class="resizeall btmiddle"/><span class="btright"></span></div>
							
						<div class="cleared"></div>
						
			  <input type="hidden" name="Identifiant_agent" value="" />
			  <input type="hidden" name="MM_insert" value="form1" />
			</form>
		</div>
</body>
</html>
<?php
mysql_free_result($rs);
mysql_free_result($rs_unite);
mysql_free_result($rs_unit_liste);
?>
<?php ob_end_flush();?>